<html> 
<head> 
<title> </title>
<style> 
px {font-family: "Lucida Console", Monaco }
p { font-size:100%; line-height:1.1em; }
body {xfont-style: sans-serif}
body {
color:#333; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:1em; line-height:1.3em; }
a:visited { color:#666; }
h1,h2,h3,h4,h5,h6 { color:#333; font-family:georgia, verdana, sans-serif; }
h1 { font-size:150%; page-break-before:auto;background-color: #aaaaff}
h2 { font-size:143%;color:teal; }
h3 { font-size:134%;color:blue; }
h4 { font-size:120%;color:gray; }
img { max-width: 55em}
p{ padding: 0;margin:0; }
p{ padding-right:1.4em; }
p{ padding-bottom:0.1em; }
p{ padding-top:1em; }
p{ whitespace: pre-wrap; }
h5,h6 { font-size:100% }
a.nav,a:link.nav, a:visited.nav { background-color:#FFF; color:#000; }
XXtable { border:double #000; border-collapse:collapse; }
XXtable { border-collapse:collapse; }
XXtd { border:thin solid #888; }
XXtd { border:none; }
li { margin-bottom:0.5em; }
blockquote { display:block; font-size:100%; line-height:1.1em; margin:0 0 0.5em; padding:0 2.5em; }
pre { background-color:#DDD; font-size:100%; overflow:auto; padding:1em; }
a,li span { color:#000; }
a:hover, a.nav:hover, a:hover math { background-color:#000; color:#FFF; }
#Container { margin:0 10px; text-align:center; background-color: #BBB}
#Content { border-top:none; margin:auto; padding:0.3em; text-align:left; width:100%; max-width:55em; background:#FFF}
span.webName { font-size:.5em; }
textarea#content { font-size: 1em; line-height: 1.125; }
h1#pageName { line-height:1em; margin:0.2em 0 0.2em 0; padding:0; }
.property { color:#666; font-size:100%; }
a.existingWikiWord[title]{ //border: 1px dashed #BBB; }
.byline { color:#666; font-size:1.0em; font-style:italic; margin-bottom:1em; padding-top:1px; } 
</style> 
</head>
<BODY bgcolor=#F0F0F0 text=#000000 link=#0000ff vlink=#C000C0 alink=#ff0000><A NAME=top></A>
<h5><a href="https://github.com/urbanjost/M_strings">[UP]</a></h5>
<div id="Container">
<div id="Content">
<CENTER>
<H1><HR><I>Manual Reference Pages &nbsp;-&nbsp;</I><NOBR>replace (3m_strings)</NOBR><HR></H1>
</CENTER>
<A name=0>

     <H3>NAME</H3>

</A>
<BLOCKQUOTE>
<B>replace</B>(3f) - [M_strings:EDITING] function replaces one
substring for another in string
(LICENSE:PD)
<P>
</BLOCKQUOTE>
<A name=contents></A><H3>CONTENTS</H3></A>
<BLOCKQUOTE>
<A HREF=#1>Synopsis</A><BR>
<A HREF=#2>Description</A><BR>
<A HREF=#3>Options</A><BR>
<A HREF=#4>Returns</A><BR>
<A HREF=#5>Examples</A><BR>
<A HREF=#6>Author</A><BR>
<A HREF=#7>License</A><BR>
</BLOCKQUOTE>
<A name=8>

     <H3>SYNOPSIS</H3>

</A>
<BLOCKQUOTE>
syntax:
<P>
<PRE>
     function replace(targetline,old,new,cmd,&
      & occurrence, &
      & repeat, &
      & ignorecase, &
      & ierr) result (newline)
     character(len=*)                       :: targetline
     character(len=*),intent(in),optional   :: old
     character(len=*),intent(in),optional   :: new
     character(len=*),intent(in),optional   :: cmd
     integer,intent(in),optional            :: occurrence
     integer,intent(in),optional            :: repeat
     logical,intent(in),optional            :: ignorecase
     integer,intent(out),optional           :: ierr
     character(len=:),allocatable           :: newline
<P>
</PRE>
</BLOCKQUOTE>
<A name=2>

     <H3>DESCRIPTION</H3>

</A>
<BLOCKQUOTE>
Replace one substring for another in string.
Either CMD or OLD and NEW must be specified.
</BLOCKQUOTE>
<A name=3>

     <H3>OPTIONS</H3>

</A>
<BLOCKQUOTE>
<TABLE cellpadding=3>
<TR valign=top><TD colspan=2>
<B>targetline</B> </TD></TR><TR valign=top><TD width=6%>&nbsp;</TD><TD>
input line to be changed
</TD></TR>
<TR valign=top><TD width=6% nowrap>
<B>old</B> </TD><TD valign=bottom>
old substring to replace
</TD></TR>
<TR valign=top><TD width=6% nowrap>
<B>new</B> </TD><TD valign=bottom>
new substring
</TD></TR>
<TR valign=top><TD width=6% nowrap>
<B>cmd</B> </TD><TD valign=bottom>
alternate way to specify old and new string, in
the form c/old/new/; where "/" can be any character
not in "old" or "new".
</TD></TR>
<TR valign=top><TD colspan=2>
<B>occurrence</B> </TD></TR><TR valign=top><TD width=6%>&nbsp;</TD><TD>
if present, start changing at the Nth occurrence of the
OLD string. If negative start replacing from the left
end of the string.
</TD></TR>
<TR valign=top><TD width=6% nowrap>
<B>repeat</B> </TD><TD valign=bottom>
number of replacements to perform. Defaults to a global
replacement.
</TD></TR>
<TR valign=top><TD colspan=2>
<B>ignorecase</B> </TD></TR><TR valign=top><TD width=6%>&nbsp;</TD><TD>
whether to ignore ASCII case or not. Defaults
to .false. .
</TD></TR>
<TR></TR></TABLE></BLOCKQUOTE>
<A name=4>

     <H3>RETURNS</H3>

</A>
<BLOCKQUOTE>
<TABLE cellpadding=3>
<TR valign=top><TD colspan=2>
<B>newline</B> </TD></TR><TR valign=top><TD width=6%>&nbsp;</TD><TD>
allocatable string returned
</TD></TR>
<TR valign=top><TD width=6% nowrap>
<B>ierr</B> </TD><TD valign=bottom>
error code. iF ier = <B>-1</B> bad directive, &gt;= 0 then
count of changes made.
</TD></TR>
<TR></TR></TABLE></BLOCKQUOTE>
<A name=5>

     <H3>EXAMPLES</H3>

</A>
<BLOCKQUOTE>
Sample Program:
<P>
<PRE>
   program demo_replace
   use M_strings, only : replace
   implicit none
   character(len=:),allocatable :: line
<P>
   write(*,*)replace(&#146;Xis is Xe string&#146;,&#146;X&#146;,&#146;th&#146;)
   write(*,*)replace(&#146;Xis is xe string&#146;,&#146;x&#146;,&#146;th&#146;,ignorecase=.true.)
   write(*,*)replace(&#146;Xis is xe string&#146;,&#146;X&#146;,&#146;th&#146;,ignorecase=.false.)
<P>
   ! a null old substring means "at beginning of line"
   write(*,*) replace(&#146;my line of text&#146;,&#146;&#146;,&#146;BEFORE:&#146;)
<P>
   ! a null new string deletes occurrences of the old substring
   write(*,*) replace(&#146;I wonder i ii iii&#146;,&#146;i&#146;,&#146;&#146;)
<P>
   ! Examples of the use of RANGE
<P>
   line=replace(&#146;aaaaaaaaa&#146;,&#146;a&#146;,&#146;A&#146;,occurrence=1,repeat=1)
   write(*,*)&#146;replace first a with A [&#146;//line//&#146;]&#146;
<P>
   line=replace(&#146;aaaaaaaaa&#146;,&#146;a&#146;,&#146;A&#146;,occurrence=3,repeat=3)
   write(*,*)&#146;replace a with A for 3rd to 5th occurrence [&#146;//line//&#146;]&#146;
<P>
   line=replace(&#146;ababababa&#146;,&#146;a&#146;,&#146;&#146;,occurrence=3,repeat=3)
   write(*,*)&#146;replace a with null instances 3 to 5 [&#146;//line//&#146;]&#146;
<P>
   line=replace( &
    & &#146;a b ab baaa aaaa aa aa a a a aa aaaaaa&#146;,&
    & &#146;aa&#146;,&#146;CCCC&#146;,occurrence=-1,repeat=1)
   write(*,*)&#146;replace lastaa with CCCC [&#146;//line//&#146;]&#146;
<P>
   write(*,*)replace(&#146;myf90stuff.f90.f90&#146;,&#146;f90&#146;,&#146;for&#146;,occurrence=-1,repeat=1)
   write(*,*)replace(&#146;myf90stuff.f90.f90&#146;,&#146;f90&#146;,&#146;for&#146;,occurrence=-2,repeat=2)
<P>
   end program demo_replace
<P>
</PRE>
Results:
<P>
<PRE>
    this is the string
    this is the string
    this is xe string
    BEFORE:my line of text
    I wonder
    replace first a with A [Aaaaaaaaa]
    replace a with A for 3rd to 5th occurrence [aaAAAaaaa]
    replace a with null instances 3 to 5 [ababbb]
    replace lastaa with CCCC [a b ab baaa aaaa aa aa a a a aa aaaaCCCC]
    myf90stuff.f90.for
    myforstuff.for.f90
<P>
</PRE>
</BLOCKQUOTE>
<A name=6>

     <H3>AUTHOR</H3>

</A>
<BLOCKQUOTE>
John S. Urban
</BLOCKQUOTE>
<A name=7>

     <H3>LICENSE</H3>

</A>
<BLOCKQUOTE>
Public Domain
</BLOCKQUOTE>
<P><HR>
<TABLE width=100%><TR> <TD width=33%><I>Nemo Release 3.1</I></TD> <TD width=33% align=center>replace (3m_strings)</TD> <TD align=right width=33%><I>April 30, 2022</I></TD> </TR></TABLE><FONT SIZE=-1>Generated by <A HREF="http://www.squarebox.co.uk/download/manServer.shtml">manServer 1.08</A> from d3d24ac9-b1cc-445e-89a2-43618fedc925 using man macros.</FONT>
<br><br><center><img src="images/replace.gif"></center>
</div>
</div>
</body>
</HTML>
